package com.utils;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * druid
 * jdbc工具类
 */
public class jdbcUtils {
private static DruidDataSource druid;
    static{
        Properties pro  =new Properties();
        try {
            InputStream in =
                    jdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //加载jdbc配置信息
            pro.load(in);
            //创建工厂类
            druid = (DruidDataSource)DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *获取链接
     * @return connecion
     */
    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = druid.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn){
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
